package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.entity.User;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;


@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    UserMapper userMapper;

    /**
     * 查询用户信息
     * @param name  用户名
     * @return
     */
    @Override
    public Optional<User> selectOne(String name) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getStatus, 1)  // 有效
                .eq(User::getName, name);   // 用户名
        Optional<User> user = Optional.ofNullable(userMapper.selectOne(wrapper));
        return user;
    }

    /**
     * 查询用户信息
     * @param name  用户名
     * @param password  密码
     * @return
     */
    @Override
    public Optional<User> selectOneByNameAndPassword(String name, String password) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getStatus, 1)  // 有效
                .eq(User::getName, name)    // 用户名
                .eq(User::getPassword, password);   // 密码
        Optional<User> user = Optional.ofNullable(userMapper.selectOne(wrapper));
        return user;
    }
}
